package com.google.sydym6.logic;

import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.google.sydym6.logic.database.IRadioDAO;
import com.google.sydym6.logic.database.RadioDAO;
import com.google.sydym6.logic.database.data.IRadioData;
import com.google.sydym6.logic.exceptions.LastfmLoginException;
import com.google.sydym6.logic.managers.IRadioManager;
import com.google.sydym6.logic.managers.ISongManager;
import com.google.sydym6.logic.managers.RadioManager;
import com.google.sydym6.logic.managers.SongManager;
import com.google.sydym6.logic.managers.recognizing.IScrobbler;
import com.google.sydym6.logic.managers.recognizing.Scrobbler;
import com.perm.kate.api.Api;

public class RadioScrobblerModel implements IRadioScrobblerModel{
	
	private static final String SAVE_LASTFM_INFO_PREF = "save lastfm info pref";
	private static final String LASTFM_LOGIN = "lastfm login";
	private static final String LASTFM_PASSWORD = "lastfm password";
	private static final int mode = Activity.MODE_PRIVATE;
	private static IRadioScrobblerModel instance; 
	private static Context context;
	
	private IRadioManager radioManager;
	private ISongManager songManager;
	private IRadioDAO radioDAO;
	private IScrobbler scrobbler;
	
	private SharedPreferences sharedPreferences;
	
	public static void setContext(Context con) {
		context = con;
	}
	
	public static synchronized IRadioScrobblerModel getInstance() {
		if (instance == null) {
			instance = new RadioScrobblerModel(context);
		}
		
		return instance;		
	}
	
	private RadioScrobblerModel(Context context, Api vkApi) {
		songManager = new SongManager();
		radioManager = new RadioManager(songManager, context);
		
		sharedPreferences = context.getSharedPreferences(SAVE_LASTFM_INFO_PREF, mode);
		final String login = sharedPreferences.getString(LASTFM_LOGIN, null);
		final String password = sharedPreferences.getString(LASTFM_PASSWORD, null);
		
		new Thread() {
			
			@Override
			public void run() {
				try {
					setLastFmAccount(login, password);
				} catch (LastfmLoginException e) {}
			}
			
		}.start();
		
		radioDAO = new RadioDAO(context);
	}
	
	private RadioScrobblerModel(Context context) {
		this(context, null);
	}
	
	@Override
	public IRadioManager getRadioManager() {
		return radioManager;
	}

	@Override
	public ISongManager getSongManager() {
		return songManager;
	}

	@Override
	public List getHistory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<IRadioData> getFavourites() {
		return radioDAO.getFavourites();
	}

	@Override
	public List<IRadioData> getCatalog() {
		return radioDAO.getCatalog();
	}

	@Override
	public IRadioData searchRadioDataById(long id) {
		Set<IRadioData> list = radioDAO.getRadioData();
		
		for (IRadioData data : list)
			if (data.getId() == id)
				return data;
		
		return null;
	}
	
	@Override
	public void setLastFmAccount(String login, String password) throws LastfmLoginException {
		scrobbler = new Scrobbler(login, password);
		radioManager.setScrobbler(scrobbler);
		songManager.setScrobbler(scrobbler);
		
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString(LASTFM_LOGIN, login);
		editor.putString(LASTFM_PASSWORD, password);
		editor.commit();
	}
	
	@Override
	public IScrobbler getScrobbler() {
		return scrobbler;
	}
}
